import Cookies from 'js-cookie'

const lang = {
  // 全局变量初始值
  state: {
    language: Cookies.get('lang') || 'zh_CN' // 项目初始化时，默认为英文
  },
  // 获取全局变量
  getters: {
    language: state => state.language
  },
  // 改变全局变量（同步）
  mutations: {
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('lang', language)
    }
  },
  // 改变全局变量（异步）
  actions: {
    setLanguage ({ commit }, language) {
      commit('SET_LANGUAGE', language)
    }
  }
}

export default lang
